package Controllers;

import Exceptions.DaoException;
import Models.PriceList;
import Interfaces.IPriceList_Dao;
import javax.swing.JOptionPane;

import DB.javaconnect;
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class PriceList_Controller {

    private IPriceList_Dao x;

    public PriceList_Controller(IPriceList_Dao x) {
        this.x = x;
    }

    PriceList_Controller() {

    }

    public PriceList Validate4(String fmpoukali, String fpoto, String fmpyra, String fsfinaki, String fmiso, String kmpoukali, String kpoto, String kmpyra, String ksfinaki, String kmiso)
            throws IllegalArgumentException, DaoException, ClassNotFoundException, SQLException {

        if (fmpoukali.isEmpty() || fpoto.isEmpty() || fmpyra.isEmpty() || fsfinaki.isEmpty() || fmiso.isEmpty() || kmpoukali.isEmpty() || kpoto.isEmpty() || kmpyra.isEmpty() || ksfinaki.isEmpty() || kmiso.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Empty Field");

        } else if (fmpoukali.matches("[0-9]+") == false || fpoto.matches("[0-9]+") == false || fmpyra.matches("[0-9]+") == false || fsfinaki.matches("[0-9]+") == false || fmiso.matches("[0-9]+") == false || kmpoukali.matches("[0-9]+") == false || kpoto.matches("[0-9]+") == false || kmpyra.matches("[0-9]+") == false || ksfinaki.matches("[0-9]+") == false || kmiso.matches("[0-9]+") == false) {
            JOptionPane.showMessageDialog(null, "There is a non-digit field.");
        } else {
            JOptionPane.showMessageDialog(null, "Info Entered");
        }
        try {
         
            Connection conn=null;
            conn=javaconnect.ConnecrDb("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/nlr");
            
           
        
            String sql;
            int ownid=2; //// apo edw vriskoume epilegoume ton owner
             sql = "INSERT INTO pricelist ( ownerID, fmpoukali, fmpoukalimiso, fpoto, fmpira, fsfinaki, kmpoukali, kmpoukalimiso, kpoto, kmpira, ksfinaki) VALUES ( "+ownid+","+fmpoukali+","+fmiso+","+fpoto+","+fmpyra+","+fsfinaki+","+kmpoukali+","+kmiso+","+kpoto+","+ksfinaki+","+kmpyra+")";
             Statement stmt1;
             stmt1=(Statement) conn.createStatement();
             stmt1.executeUpdate(sql);

            return x.ValidatePrices(fmpoukali, fpoto, fmpyra, fsfinaki, fmiso, kmpoukali, kpoto, kmpyra, ksfinaki, kmiso);
        } catch (DaoException e) {
            throw e;
        }

    }
}
